Imports System.Drawing
Imports System.Globalization
Imports System.Windows.Forms
Imports Vintasoft.Pdf
Imports Vintasoft.Pdf.Tree
Imports Vintasoft.Imaging

Namespace Pdf
	Public Partial Class AddEmptyPageForm
		Inherits Form

		#Region "Fields"

		Private _updateValues As Boolean = False

		#End Region



		#Region "Constructor"

		Public Sub New(initalPageSizeInUserUnits As SizeF, initalUnits As UnitOfMeasure)
			InitializeComponent()
			_pageSizeInUserUnits = initalPageSizeInUserUnits
			unitsComboBox.Items.Add(UnitOfMeasure.Inches)
			unitsComboBox.Items.Add(UnitOfMeasure.Centimeters)
			unitsComboBox.Items.Add(UnitOfMeasure.Millimeters)
			unitsComboBox.Items.Add(UnitOfMeasure.Pixels)
			unitsComboBox.Items.Add(UnitOfMeasure.PdfUserUnits)
			unitsComboBox.SelectedItem = initalUnits

			paperKindComboBox.Items.AddRange(GetEnumValues(GetType(PaperSizeKind)))
			paperKindComboBox.Items.Remove(PaperSizeKind.[Custom])
			paperKindComboBox.SelectedItem = PaperSizeKind.A4

			standardSizeRadioButton.Checked = True
		End Sub

		#End Region



		#Region "Properties"

		Private _pageSizeInUserUnits As SizeF
		Public ReadOnly Property PageSizeInUserUnits() As SizeF
			Get
				Return _pageSizeInUserUnits
			End Get
		End Property

		Public ReadOnly Property Units() As UnitOfMeasure
			Get
				Return DirectCast(unitsComboBox.SelectedItem, UnitOfMeasure)
			End Get
		End Property

		Private _paperKind As PaperSizeKind = PaperSizeKind.[Custom]
		Public ReadOnly Property PaperKind() As PaperSizeKind
			Get
				Return _paperKind
			End Get
		End Property

		Public ReadOnly Property Rotated() As Boolean
			Get
				Return rotatedCheckBox.Checked
			End Get
		End Property

		#End Region



		#Region "Methods"

		Private Function GetEnumValues(type As Type) As Object()
			Dim ar As Array = [Enum].GetValues(type)
			Dim result As Object() = New Object(ar.Length - 1) {}
			ar.CopyTo(result, 0)
			Return result
		End Function

		Private Sub UpdateValues()
			_updateValues = True
			widthTextBox.Text = FloatToString(PdfPage.ConvertFromUserUnitsToUnitOfMeasure(PageSizeInUserUnits.Width, Units))
			heightTextBox.Text = FloatToString(PdfPage.ConvertFromUserUnitsToUnitOfMeasure(PageSizeInUserUnits.Height, Units))
			_updateValues = False
		End Sub


		Private Shared Function FloatToString(value As Single) As String
			Return value.ToString("f2", CultureInfo.InvariantCulture)
		End Function


		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			If customSizeRadioButton.Checked Then
				_paperKind = PaperSizeKind.[Custom]
			Else
				_paperKind = DirectCast(paperKindComboBox.SelectedItem, PaperSizeKind)
			End If
			DialogResult = DialogResult.OK
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		Private Sub unitsComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
			UpdateValues()
		End Sub

		Private Sub heightTextBox_TextChanged(sender As Object, e As EventArgs)
			If _updateValues Then
				Return
			End If
			Try
				_pageSizeInUserUnits.Height = PdfPage.ConvertFromUnitOfMeasureToUserUnits(Single.Parse(heightTextBox.Text, CultureInfo.InvariantCulture), Units)
			Catch exc As Exception
				MessageBox.Show(exc.Message, "Error")
			End Try
		End Sub

		Private Sub widthTextBox_TextChanged(sender As Object, e As EventArgs)
			If _updateValues Then
				Return
			End If
			Try
				_pageSizeInUserUnits.Width = PdfPage.ConvertFromUnitOfMeasureToUserUnits(Single.Parse(widthTextBox.Text, CultureInfo.InvariantCulture), Units)
			Catch exc As Exception
				MessageBox.Show(exc.Message, "Error")
			End Try
		End Sub

		Private Sub customSizeRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			groupBox1.Enabled = customSizeRadioButton.Checked
			groupBox2.Enabled = Not groupBox1.Enabled
		End Sub

		Private Sub radioButton2_CheckedChanged(sender As Object, e As EventArgs)
			groupBox2.Enabled = standardSizeRadioButton.Checked
			groupBox1.Enabled = Not groupBox2.Enabled
		End Sub

		Private Sub paperKindComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
			If standardSizeRadioButton.Checked Then
				Dim size As ImageSize = ImageSize.FromPaperKind(DirectCast(paperKindComboBox.SelectedItem, PaperSizeKind), Resolution.ScreenResolution)
				Dim width As Single = CSng(PdfPage.ConvertFromUnitOfMeasureToUserUnits(CSng(size.WidthInInch), UnitOfMeasure.Inches))
				width = PdfPage.ConvertFromUserUnitsToUnitOfMeasure(width, DirectCast(unitsComboBox.SelectedItem, UnitOfMeasure))
				Dim height As Single = CSng(PdfPage.ConvertFromUnitOfMeasureToUserUnits(CSng(size.HeightInInch), UnitOfMeasure.Inches))
				height = PdfPage.ConvertFromUserUnitsToUnitOfMeasure(height, DirectCast(unitsComboBox.SelectedItem, UnitOfMeasure))
				widthTextBox.Text = width.ToString(CultureInfo.InvariantCulture)
				heightTextBox.Text = height.ToString(CultureInfo.InvariantCulture)
			End If
		End Sub

		#End Region

	End Class
End Namespace
